
%shift = 1 in case of mmusic otherwise 0
function plot_figure (path, shift)

nProc_i = [1024, 8192];
for i=1:2
pp=strcat(path, int2str(nProc_i(i)));

[cmt, cpt, tt, osp, isp] = getvalues(pp, shift);

if size(osp,1)==1
    plot_times(cpt,cmt,tt,i,nProc_i(i));
else
    plot_spikes(isp,osp,i, nProc_i(i));
end
end

end

function plot_spikes(i_sp, o_sp, nsubpl, nProc)
x = 1:197;
nCells = 2*1024*1024;
coef1 = nProc/nCells;
coef2 = coef1/1000;

osp = o_sp(1,:);%max(msg_out);
isp = i_sp(1,:);%max(msg_in);

subplot(2,1,nsubpl),   
plot(x,osp*coef1,'b'), hold on, 
plot(x,isp*coef2,'g'), hold on, 
ylim([0,0.25]),
xlim([0,200]),
grid on, %title(ttl)
xlabel('interval number'), ylabel('Fraction of spikes on the processor #0');

end

function plot_times(cmp, cmm, tt, nsubpl, nProc)
x = 1:197;
m_cmp=mean(cmp);
m_tt=mean(tt);

max_cmp=max(cmp);
max_tt=max(tt);

subplot(2,1,nsubpl),   
plot(x, m_cmp,'k'), hold on, 
plot(x, max_cmp,'r'), hold on, 
plot(x,m_tt,'k'), hold on, 
plot(x,max_tt,'r'), hold on,

%ylim([0,25]),
xlim([0,200]),
grid on, %title(ttl)
xlabel('interval number'), ylabel('Interval duration (ms)');
end

function [cmt, cpt, tt, osp, isp] = getvalues(path, add)
path
cmt = loadfile(strcat(path,'/ctime'),3);
tt = loadfile(strcat(path,'/ctimes'),3+add);
cmt = cmt./1000.0;%/1.17647;
tt = tt./1000.0;

osp = loadfile(strcat(path,'/out_spikes'),3);
isp = loadfile(strcat(path,'/in_spikes'),3);
cpt = tt-cmt;

end

function data = loadfile(filename, start)
fin = 196+start;
if exist(filename,'file')
    data = load(filename);
    data = data(:,start:fin);
else
    strcat('file not found:',filename)
    data = zeros(1,fin-start+1);
end
end
